package com.cbtx.module.base.ui

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.fragment.app.FragmentActivity

abstract class BaseActivity : FragmentActivity() {

    lateinit var mActivity: Activity
    lateinit var mContext: Context

    companion object {
        var isCanShowAdv = false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //全屏显示
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN
        )


        //沉浸状态栏
//        setStatusBar()
        //状态栏字体颜色默认都给它显示为“灰色”
//        setAndroidNativeLightStatusBar(true)

        mActivity = this
        setContentView(getLayoutId())

        hideBottomButton()
        initView(savedInstanceState)

        getData()
    }


    abstract fun getLayoutId(): Int

    abstract fun initView(savedInstanceState: Bundle?)

    abstract fun onMyViewClick(view: View)

    abstract fun getData()

    fun addClickListener(view: View) {
        //防止暴力点击，统一处理
        view.setOnClickListener(object : MyOnClickListener() {
            override fun onMyClick(view: View) {
                onMyViewClick(view)
            }
        })
    }

    fun addClickListenerNormal(view: View) {
        //不需要防止暴力点击
        view.setOnClickListener {
            onMyViewClick(view)
        }
    }

    override fun onResume() {
        super.onResume()

        isCanShowAdv = true
    }

    /**
     * 沉浸状态栏
     */
    @SuppressLint("NewApi")
    fun setStatusBar() {
        val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        window.decorView.systemUiVisibility = option
        window.statusBarColor = Color.TRANSPARENT //也可以设置成灰色透明的，比较符合Material Design的风格
    }

    /**
     * 设置状态栏的字体颜色是否高亮
     * @param dark true:会显示灰色 : false :会显示白色（亮色）
     */
    fun setAndroidNativeLightStatusBar(dark: Boolean) {
        val decor = window.decorView
        if (dark) {
            decor.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        } else {
            decor.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        }
    }


    private fun hideBottomButton() {
        val window = window
        val params = window.attributes
        params.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE
        window.attributes = params
    }

}